<template>
	<div class="evidence-unit-case-container">
		<el-form ref="refSeach" :inline="true" :model="formInline" size="mini">
			<el-form-item label-width="100px" prop="caseNo" label="文书编号">
				<el-input v-model="formInline.caseNo" placeholder="请输入..."></el-input>
			</el-form-item>
			<el-form-item label-width="100px" prop="inspectionNo" label="案件性质">
				<el-input v-model="formInline.inspectionNo" placeholder="请输入..."></el-input>
			</el-form-item>
			<el-form-item label-width="100px" prop="caseName" label="案件名称">
				<el-input v-model="formInline.caseName" placeholder="请输入..."></el-input>
			</el-form-item>
			<el-form-item label-width="100px" prop="caseTime" label="案发时间">
				<el-date-picker
					v-model="formInline.caseTime"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					type="daterange"
					start-placeholder="开始时间"
					end-placeholder="结束时间"
					style="width: 220px"
				>
				</el-date-picker>
			</el-form-item>
			<el-form-item label-width="100px" prop="hostUserName" label="主办干警">
				<el-input v-model="formInline.hostUserName" placeholder="请输入..."></el-input>
			</el-form-item>
			<el-form-item label-width="100px" prop="hostUserOrganName" label="主办单位">
				<el-input v-model="formInline.hostUserOrganName" placeholder="请输入..."></el-input>
			</el-form-item>
			<el-form-item style="margin-left: 20px">
				<el-button type="primary" @click="onSearch" icon="el-icon-search" size="small">查询</el-button>
				<el-button type="primary" @click="onRest" icon="el-icon-view" size="small">重置</el-button>
			</el-form-item>
		</el-form>
		<el-table
			ref="refCaseTable"
			:data="tableData"
			:default-sort="{ prop: 'name', order: 'descending' }"
			:row-style="{ height: '0' }"
			:cell-style="{ padding: '0' }"
			size="medium"
			style="width: 100%"
			highlight-current-row
			:row-class-name="tableRowClassName"
			@row-dblclick="rowDblclick"
			@selection-change="handleSelectionChange"
		>
			<el-table-column align="center" type="selection" width="55" />
			<el-table-column align="center" label="序号" type="index" width="60"> </el-table-column>
			<el-table-column align="center" property="caseNo" sortable label="文书编号" />
			<el-table-column align="center" property="inspectionNo" sortable label="案件性质" show-overflow-tooltip />
			<el-table-column align="center" property="caseName" sortable label="案件名称" show-overflow-tooltip />
			<el-table-column align="center" sortable label="案发时间" show-overflow-tooltip>
				<template #default="scope">
					{{ dateFormat(scope.row.caseTime) }}
				</template>
			</el-table-column>
			<el-table-column align="center" property="hostUserName" sortable label="主办干警" show-overflow-tooltip />
			<el-table-column align="center" property="hostUserOrganName" sortable label="主办单位" show-overflow-tooltip />
			<el-table-column align="center" label="操作" width="145">
				<template #default="scope">
					<el-button type="primary" @click="onEditCaseInfo(scope.row)" plain size="mini">修改</el-button>
					<el-button type="success" @click="onDetailsInfo(scope.row)" plain size="mini">详情</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:page-sizes="[10, 20, 50, 100]"
			:page-size="formInline.pageSize"
			layout="prev, pager, next, jumper,total,sizes"
			:total="total"
			@size-change="onHandleSizeChange"
			@current-change="onHandleCurrentChange"
			prev-text="上一页"
			next-text="下一页"
			background
		>
		</el-pagination>
		<EditCase ref="editCaseRef" :formInline="formInlineCase" @reload="init" />
	</div>
</template>

<script lang="ts">
import { ref, reactive, toRefs } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import EditCase from '/@/views/evidence/unit/component/editCase.vue';
import { getCasePager } from '/@/api/case/index';
import { dateFormat } from '/@/common';
import { sendPandian } from '/@/api/common/index';
import { getCaseEvidenceByIds,savePanDianTask  } from '/@/api/case/index';

export default {
	name: 'EvidenceUnitCase',
	components: { EditCase },
	setup() {
		const router = useRouter();
		const editCaseRef = ref();
		
		const state = reactive({
			formInline: {
				caseNo: '',
				inspectionNo: '',
				caseName: '',
				caseTime: '',
				hostUserName: '',
				hostUserOrganName: '',
				page: 1,
				pageSize: 10,
			},
			checkRecords: [],
			formInlineCase: {},
			tableData: [],
			total: 0,
		});
		
	
		const onEditCaseInfo = (row) => {
			Object.assign(state.formInlineCase, row);
			editCaseRef.value.openDialog();
		};
		const onDetailsInfo = (row: any) => {
			router.push({
				path: '/evidence/unit/details',
				query: { caseid: row.id },
			});
		};
		function tableRowClassName({ row, rowIndex }) {
			if (rowIndex === 1) {
				return 'warning-row';
			} else if (rowIndex === 3) {
				return 'success-row';
			}
			return '';
		}
		async function init() {
			state.formInline.CaseTimeStart = '';
			state.formInline.CaseTimeEnd = '';
			if (state.formInline.caseTime && state.formInline.caseTime.length > 1) {
				state.formInline.CaseTimeStart = state.formInline.caseTime[0];
				state.formInline.CaseTimeEnd = state.formInline.caseTime[1];
			}
			await getCasePager(state.formInline).then((res: any) => {
				if (res.success) {
					state.tableData = res.data.data;
					state.total = res.data.totalCount;
				}
			});
		}
		const refSeach = ref();
		function onSearch() {
			init();
		}
		function onRest() {
			refSeach.value.resetFields();
		}
		const refCaseTable = ref();
		function getCheckRecords() {
			return state.checkRecords;
		}
		function handleSelectionChange(value) {
			state.checkRecords = value;
		}
		function onHandleSizeChange(pageSize: number) {
			state.formInline.pageSize = pageSize;
			init();
		}
		function onHandleCurrentChange(pageIndex: any) {
			state.formInline.page = pageIndex;
			init();
		}
		function rowDblclick(row: any) {
			router.push({
				path: '/evidence/unit/details',
				query: { caseid: row.id },
			});
		}

		function getRand(min, max) {
			var rand = Math.random();
			return Math.round(min + rand * (max - min));
		}

		async function getEviByCaseId() {
			if (state.checkRecords.length == 0) {
				ElMessage({
					message: '请勾选案件进行盘点',
					type: 'warning',
				});

				return;
			}
			var eviList = new Array();
			var ids = state.checkRecords.map(function(a){ return a.id;});
			await getCaseEvidenceByIds(ids).then((res: any) => {
				if (res.success) {
					for (var k = 0; k < res.data.evidences.length; k++) {
						var _data = res.data.evidences[k];
						if(_data.rfid){
							eviList.push({ evid:_data.id,evidenceName: _data.name, address: _data.location, epc: _data.rfid, registerState: 1 });
						}
					}
					if(eviList.length == 0){
						ElMessage({
							message: '无可盘点物品！',
							type: 'warning',
						});
					}else{
						//pandian(eviList);
						pandianForMR730(eviList);
					}
					
				}
			});
		}

		async function pandian(taskdata) {
			var num = getRand(100000, 999999);

			var data = { taskId: num, taskName: num + '盘点计划', type: 0, taskData: taskdata };

			await sendPandian(JSON.stringify(data)).then((res) => {
				var _result = JSON.parse(res.data);
				if (res.success && _result.success) {
					ElMessage({ message: num + '盘点计划,发送成功', type: 'success' });
				} else {
					ElMessage.error(_result.message);
				}
			});
		}
		async function pandianForMR730(taskdata) {
			var ids = taskdata.map(function(a){ return a.evid;});

			ElMessageBox.prompt('请输入盘点计划名称：', '盘点', {
				confirmButtonText: '确定',
				cancelButtonText: '取消'
			}).then(({ value }) => {
				if(value){
					var data = { name: value, ids: ids };
					savePanDianTask(JSON.stringify(data)).then((res) => {
						if (res.success) {
							ElMessage({ message: '盘点计划,添加成功', type: 'success' });
						} else {
							ElMessage.error(res.msg);
						}
					});
				}
			}).catch(() => {
				//取消
			}); 
		}
		init();
		return {
			getEviByCaseId,
			editCaseRef,
			onEditCaseInfo,
			onDetailsInfo,
			tableRowClassName,
			dateFormat,
			pandian,
			init,
			onSearch,
			onRest,
			refCaseTable,
			getCheckRecords,
			handleSelectionChange,
			onHandleSizeChange,
			onHandleCurrentChange,
			refSeach,
			rowDblclick,
			...toRefs(state),
		};
	},
};
</script>
<style scoped>
.el-input,
.el-select,
.el-date-picker {
	width: 220px;
}
</style>
